package itbour.onetouchshow.widget.inching;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

import itbour.onetouchshow.utils.L_;

/**
 * Created by zjl_d on 2017/11/24.
 */

public class InchingBaseAreaRelativeLayout extends RelativeLayout {

    //出血区域w
    private int bw;
    //出血区域h
    private int bh;
    //控制图标的宽
    private int cw;
    //控制图标的高
    private int ch;
    //本控件实际绘制区域的宽
    private int w;
    //本控件实际绘制区域的高
    private int h;
    //移除容器距离实际绘图区域的宽高
    private int topZone;
    private int leftZone;
    //对于可操作控件上下左右边界 非控制点可移动范围
    private int lB;
    private int tB;
    private int bB;
    private int rB;
    private int maxW;
    private int maxH;

    public InchingBaseAreaRelativeLayout(Context context) {
        this(context, null);
    }

    public InchingBaseAreaRelativeLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public InchingBaseAreaRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public int getBw() {
        return bw;
    }

    public void setBw(int bw) {
        this.bw = bw;
    }

    public int getBh() {
        return bh;
    }

    public void setBh(int bh) {
        this.bh = bh;
    }

    public int getCw() {
        return cw;
    }

    public void setCw(int cw) {
        this.cw = cw;
    }

    public int getCh() {
        return ch;
    }

    public void setCh(int ch) {
        this.ch = ch;
    }

    public int getMaxW() {
        return maxW;
    }

    public int getMaxH() {
        return maxH;
    }

    public int getTopZone() {
        return topZone;
    }

    public int getLeftZone() {
        return leftZone;
    }

    public void setParams(int bw, int bh, int cw, int ch, int w, int h, int topZone, int leftZone) {
        this.bw = bw;
        this.bh = bh;
        this.cw = cw;
        this.ch = ch;
        this.w = w;
        this.h = h;
        this.topZone = topZone;
        this.leftZone = leftZone;

//        this.lB = cw / 2 + 2 * bw;
//        this.bB = topZone + h + ch / 2 - 2 * bh;
//        this.rB = cw / 2 + w + cw / 2 - 2 * bw;
//        this.tB = topZone + 2 * bh;

        this.lB = 0;
        this.bB = h + ch / 2;
        this.rB = w + cw / 2;
        this.tB = 0;
        L_.i("newParams rb===" + this.rB + "this.bB===" + this.bB);

        this.maxW = w - 2 * bw;
        this.maxH = h - 2 * bh;

        L_.i("setParams===w" + w + "h===" + h + "maxW===" + maxW + "maxH===" + maxH);
    }

    public int getlB() {
        return lB;
    }

    public int gettB() {
        return tB;
    }

    public int getbB() {
        return bB;
    }

    public int getrB() {
        return rB;
    }

    /**
     * 绘制网格线和出血区域
     */
    public void setMaskAndGrid() {
//        initGridView();
        if (bw > 0 && bh > 0) {
            initBleedMask();
        }
    }

    private void initBleedMask() {
        //在创建定位模版位置时创建一个蒙版宽高和一致
        InchingBleedMask printInchingMask = new InchingBleedMask(getContext());

        //这里要分别设置出血区域的宽和高
        printInchingMask.setBleedWidthAndHeight(bw, bh);

        //出血区域的绘制要减去半个控制图标的高
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(w, h);
        this.addView(printInchingMask, params);

        //设置为不可获取焦点
        printInchingMask.setFocusable(false);
    }

    private void initGridView() {
        GridLayerView gridLayerView = new GridLayerView(getContext());
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(w, h);
        this.addView(gridLayerView, params);
        //设置为不可获取焦点
        gridLayerView.setFocusable(false);
    }
}
